import { API } from './BaseApi'
import Utils from '../../vue2-flow/src/utils/Common'
import { Device } from "@/api/DeviceApi";

class SysPlcDriverAddressApi {
  /**
   * 添加PLC地址
   * @param AddressInfo
   * @param callback
   * @returns {Promise<void>}
   * @constructor
   */
  async Install(AddressInfo, callback) {
    const nd = await Device.UpdateAddress(AddressInfo)
    callback(nd.Data)
  }

  /**
   * 根据设备id，获取所有地址列表
   * @param callback
   * @returns {Promise<void>}
   * @constructor
   */
  async Query(DriverId, callback) {
    const Res = await Device.GetPlcAddressByDriverId(DriverId)
    if (Utils.IsEmpty(Res)) {
      callback([])
    }
    callback(Res.Data)
  }

  async Get(Id, callback) {
    const info = await Device.GetAddress(Id)
    if (Utils.IsEmpty(info)) {
      callback({})
    }
    callback(info.Data)
  }

  async QueryByDriverType(DriverType, callback) {
    const info = await API.SysPlcDriverAddressApi.QueryByDriverType(DriverType)
    if (Utils.IsEmpty(info)) {
      callback({})
    }
    callback(JSON.parse(info))
  }

  /**
   * 根据 ID 删除设备
   * @param Id
   * @param callback
   * @returns {Promise<void>}
   * @constructor
   */
  async Delete(Id, callback) {
    const de = await API.SysPlcDriverAddressApi.Delete(Id)
    callback(de)
  }
}

export default SysPlcDriverAddressApi
